<?php

class ApiController extends RestController
{

    public function actionStartUp()
    {
        $data = array();
        Helper::positionRecord('init','');
        //省市最后更新时间
        $data['provinceCityUpdate'] = '2014-11-04 16:00:00';
        //是否显示广告
        if(MOBILEOS === 'ios')
        {
            if(Yii::app()->cache2->hget('mobile_app_showAD',APPVERSION))
                $data['showAD'] = 1;
            else
                $data['showAD'] = 0;
            $info = json_decode(Yii::app()->cache->get('APP_VERSIONS'));
        }
        else
            $info = json_decode(Yii::app()->cache->get('Android_APP_VERSION'));
        //启动图最后更新时间
        $data['imageUpdate'] = $info->update_time;
        $pipeline = Yii::app()->cache_php->pipeline();
        $pipeline->get('CGTZ_APP_NOTICE');
        $pipeline->get('appRegSucImage');
        $pipeline->hget('MOBILE_APP_CONFIG','kefu');
        $pipeline->hget('MOBILE_APP_CONFIG','e');
        $pipeline->hget('MOBILE_APP_CONFIG','reward');
        $pipeline->get('appProjectStyleLastUpdate');
        if(MOBILEOS==='android' && CHANNEL)//安卓根据渠道来给启动图
        {
            $pipeline->hget('app_channels_startup_image',CHANNEL);
        }
        $dataList = $pipeline->exec();
        list($notice,$regImg,$kefu,$e,$reward,$styleUpdate,$image) = $dataList;
        if($notice)
        {
            $notice = json_decode($notice);
            if(strtotime($notice->start)<= time() && strtotime($notice->end) > time())
            {
                $data['notice_title']   = $notice->title;
                $data['notice_content'] = $notice->content;
            }
        }
        if($regImg)
        {
            $regImg = json_decode($regImg);
            if($regImg && strtotime($regImg->lastUpdate) > strtotime($data['imageUpdate']))
                $data['imageUpdate'] = $regImg->lastUpdate;
        }
        $kefu      = json_decode($kefu);
        $e         = json_decode($e);
        $reward    = json_decode($reward);
        //kefu最后更新时间
        $data['kefuUpdate']  = $kefu->lastUpdate;
        if(strtotime($kefu->lastUpdate) < strtotime($e->lastUpdate))
            $data['kefuUpdate'] = $e->lastUpdate;
        if(strtotime($reward->lastUpdate) > strtotime($data['kefuUpdate']))
            $data['kefuUpdate'] = $reward->lastUpdate;

        $data['projectStyleLastUpdate'] = $styleUpdate?$styleUpdate:'2015-05-27 12:00:00';
        
        if($image)//安卓根据渠道来给启动图
        {
            $image = json_decode($image);
            if(strtotime($image->update_time) > strtotime($info->update_time))
                $data['imageUpdate'] = $image->update_time;
        }
        $data['success']   = 1;
        $this->setResult($data);
    }
    
    /**
     * 获取客服相关内容
     */
    public function actionNewGetKefuTip()
    {
        $data   = array();
        $pipe   = Yii::app()->cache_php->pipeline();
        $pipe->hget('MOBILE_APP_CONFIG','kefu');
        $pipe->hget('MOBILE_APP_CONFIG','e');
        $pipe->hget('MOBILE_APP_CONFIG','reward');
        $dataList = $pipe->exec();
        foreach($dataList as $k=>$v)
            $dataList[$k] = json_decode($v);
        list($kefu,$e,$reward) = $dataList;
        $data['content']          = $kefu->fullDesc;
        $data['work_time']        = $kefu->worktime;
        $data['phone_number']     = $kefu->phone;
        $data['reward_use_limit'] = $reward->bonusUseLimit?$reward->bonusUseLimit:0.01;
        $data['coupon_use_limit'] = $reward->couponUseLimit?$reward->couponUseLimit:500;
        $number                   = $reward->totalAmount?$reward->totalAmount:60;
        $data['tip']              = $number.'元新人礼包';
        $data['possession_safe']  = $e->slogan?$e->slogan:'平台账户资金由国有银行实时监管';
        $data['success']          = 1;
        $this->setResult($data);
    }
    /**
     * 获取手机启动图片
     */
    public function actionNewStartImage()
    {
        $data = array();
        if(MOBILEOS=='android')
            $info = json_decode(Yii::app()->cache->get('Android_APP_VERSION'));
        else
            $info = json_decode(Yii::app()->cache->get('APP_VERSIONS'));

        $cache_php = Yii::app()->cache_php;
        $regImg = $cache_php->get('appRegSucImage');
        if($regImg)
        {
            $regImg = json_decode($regImg);
            $data['regImgUrl'] = $regImg->image;
        }
        else
            $data['regImgUrl'] = 'http://cgtzfiles.b0.upaiyun.com/appimg/xinshouyindao.png';

        if($info)
        {
            $data['url']        = $info->imgUrl;
            if(MOBILEOS=='android' && CHANNEL && $image = Yii::app()->cache_php->hget('app_channels_startup_image',CHANNEL) )//安卓根据渠道来给启动图
            {
                $image              = json_decode($image);
                $data['url']        = $image->imgUrl;
            }
            $data['success'] = 1;
            $this->setResult($data);
        }
    }

    /**
     * 获取省份城市
     */
    public function actionGetProvinceAndCities()
    {
        $data = array('success'=>0);
        $cache = Yii::app()->cache2->get('province.list');
        if($cache)
            $result = json_decode($cache);
        else
        {
            $r = HttpLib::get('spProvinces/getAll.json',array());
            if($r->success && $r->result)
                $result = $r->result;
            else
            {
                $data['msg'] = $r->errorMsg;
                $this->setResult($data);
            }
        }
        $provinces = array('values'=>array());
        $cities = array();
        foreach($result as $k=>$a)
        {
            $provinces['values'][] = $k;
            $cities[] = array(
                'province' => $k,
                'values'   => $a,
            );
        }
        $data['cities']    = $cities;
        $data['provinces'] = $provinces;
        $data['success']   = 1;
        $data['msg']       = 'ok';
        $this->setResult($data);
    }

    /**
     * 获取开户行
     */
    public function actionGetBranch()
    {
        $data = array('success'=>0,'msg'=>'');
        $province = $this->_post('province');
        $city     = $this->_post('city');
        $bankName = $this->_post('bankName');
        if(!$province || !$city || !$bankName)
        {
            $data['msg'] = 'province或者city或者bankName参数不能为空';
        }
        else
        {
            $remote = HttpLib::get($this->javaApi['shengpay_bank_branchs'],array(
                    'province'=>$this->_post('province'),
                    'city'=>$this->_post('city'),
                    'bankName'=>$this->_post('bankName'),
                ));
            $data['msg'] = $remote->errorMsg;
            $branches = array();
            if($remote->success && $remote->result->pageList)
            {
                foreach($remote->result->pageList as $branch)
                {
                    if($bankName=='中国邮储银行')
                        $bankName = '中国邮政储蓄银行';
                    elseif($bankName=='杭州联合银行')
                        $bankName = '杭州联合农村商业银行';
                    $tmp = $branch->branchName;
                    if(strpos($branch->branchName,$bankName.'股份有限公司') !== false)
                    {
                        $tmp =  mb_substr($branch->branchName, mb_strlen($bankName.'股份有限公司'));
                    }
                    elseif(strpos($branch->branchName,$bankName.'有限责任公司') !== false)
                    {
                        $tmp =  mb_substr($branch->branchName, mb_strlen($bankName.'有限责任公司'));
                    }
                    elseif(strpos($branch->branchName,$bankName) !== false)
                    {
                        $tmp =  mb_substr($branch->branchName, mb_strlen($bankName));
                    }
                    $branches[] = array(
                        'value' => $branch->branchName,
                        'show'  => $tmp,
                    );
                }
                $data['branches'] = $branches;
                $data['success']  = 1;
                $data['msg']      = 'ok';
            }
            elseif($remote->success && $remote->result=='')
            {
                $data['msg'] = '该城市下无开户行信息，请联系客服';
            }
        }
        $this->setResult($data);
    }


    /**
     * 获取可供绑定的银行
     */
    public function actionGetAvailableBanks()
    {
        $data  = array(
            'success' => 0,
            'banks'   => array(),
        );
        if(Yii::app()->cache2->get('pay_ll_closed'))
        {
            $data['success'] = -1;
            $data['msg']     = '第三方支付服务暂停，请稍后再试';
        }
        else
        {
            $data['success'] = 1;
            $data['banks']   = Helper::getAvailableBanks();
        }
        $this->setResult($data);
    }

    /**
     * 折价率
     */
    public function actionDiscountList()
    {
        $data = array(
            'success'  => 1,
            'list'     => array(number_format(0,1),0.1,0.2,0.3,0.4,0.5),
            'tRate'    => 0.005,//转让手续费率
            'question' => '我有疑问',
            'answer'   => '关于折价率
转让人可设置低于项目金额的价格进行转让，
转让价格与项目本金之差与项目本金的比率称之为折价率。
举例：您设置转让金额10000元，转让时设置折价率0.1%，则折后转让价格为9990元（=10000*（1-0.1%））

关于转让服务费
实际转让金额的0.5%作为转让服务费，平台收取。
举例：转让金额10000元，若全部转让，则最多收取服务费50元；若至结束时转让5000元，则收取服务费25元。',
        );
        $this->setResult($data);
    }
    /**
     * 苹果激活
     */
    public function actionActivation()
    {
        Helper::pushIFA('','init');
        $this->setResult(array(
            'success' => 1,
        ));
    }
    /**
     * 获取项目图标
     */
    public function actionGetProjectStyle()
    {
        $data  = array(
            'success' => 1,
            'list'    => array(),
        );
        $styles = Yii::app()->cache_php->get('appProjectStyle');
        if($styles)
        {
            $styles = json_decode($styles);
            foreach($styles as $style)
            {
                $data['list'][] = array(
                    'property' => $style->property,
                    'fontSize' => $style->fontSize,
                    'color'    => $style->color,
                    'icon'     => $style->icon,
                );
            }
        }
        $this->setResult($data);
    }
}
